import java.util.ArrayList;
import java.util.List;

public class SongFingerprint {
    private double[] song;
    private List<Fingerprint> fingerprint = null;

    public SongFingerprint(double[] song) {
        this.song = song;
    }

    public List<Fingerprint> getFingerprint() {
        if (fingerprint == null) {
            fingerprint = new ArrayList<>();
            List<double[]> datas = splitArray(song, FFT.WINDOW_SIZE);
            Fingerprint newFingerPrint = new Fingerprint();
            for (double[] dataSlice : datas) {
                newFingerPrint.append(FFT.fft(dataSlice));
                fingerprint.add(newFingerPrint);
            }
        }
        return fingerprint;
    }

    private List<double[]> splitArray(double[] oldArray, int subSize) {
        int count = oldArray.length % subSize == 0 ? oldArray.length / subSize : oldArray.length / subSize + 1;

        List<double[]> subArrays = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int index = i * subSize;

            double[] list = new double[subSize];
            int j = 0;
            while (j < subSize) {
                double value = 0.0;
                if (index < oldArray.length) {
                    value = oldArray[index++];
                } else {
                    value = 0.0;
                }
                list[j] = value;
                j++;
            }

            subArrays.add(list);
        }
        return subArrays;
    }
}
